package com.gewu.pm.ui.activity.delivery;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.bean.DeliveryGoodsBean;
import com.gewu.pm.ui.adapter.delivery.DeliveryGoodsAdapter;
import com.gewu.pm.utils.MathUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情页面
 */
public final class DeliveryGoodsDetailActivity extends AppActivity {

    private EditText mSearchEdit;
    private RecyclerView mRecyclerView;
    private TextView mVarietyCountView;
    private TextView mTotalQuantityView;
    private TextView mTotalAmountView;

    private DeliveryGoodsAdapter mAdapter;
    private List<DeliveryGoodsBean> mAllDataList = new ArrayList<>();  // 全部数据
    private String mSearchKeyword = "";  // 搜索关键词

    public static void start(Context context) {
        Intent intent = new Intent(context, DeliveryGoodsDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.delivery_goods_detail_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSearchEdit = findViewById(R.id.et_search);
        mRecyclerView = findViewById(R.id.rv_goods_list);
        mVarietyCountView = findViewById(R.id.tv_variety_count);
        mTotalQuantityView = findViewById(R.id.tv_total_quantity);
        mTotalAmountView = findViewById(R.id.tv_total_amount);

        mSearchEdit.setHint("输入商品名称/商品编码");
        // 设置 RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new DeliveryGoodsAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        // 设置搜索监听
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mSearchKeyword = s.toString().trim();
                filterData();
            }
        });
    }

    @Override
    protected void initData() {
        // 创建测试数据
        createTestData();
        
        // 显示数据
        mAdapter.setData(mAllDataList);
        
        // 更新统计
        updateStatistics();
    }

    /**
     * 创建测试数据（3条商品，价格和数量不同）
     */
    private void createTestData() {
        mAllDataList.clear();
        
        // 商品1
        DeliveryGoodsBean goods1 = new DeliveryGoodsBean(
                "祖名素肉豆干手撕素牛排素牛肉豆腐干辣条零食小吃休闲...",
                "101103002",
                "【营鲜装】300g*8瓶",
                20.0,   // 单价
                3       // 发货数量
        );
        
        // 商品2
        DeliveryGoodsBean goods2 = new DeliveryGoodsBean(
                "祖名老豆腐4000g经典原味豆制品",
                "101103003",
                "【经典装】4000g*5盒",
                35.0,   // 单价
                10      // 发货数量
        );
        
        // 商品3
        DeliveryGoodsBean goods3 = new DeliveryGoodsBean(
                "祖名浓浆豆腐400g嫩豆腐内酯豆腐",
                "101103004",
                "【营养装】400g*12盒",
                50.0,   // 单价
                7       // 发货数量
        );
        
        mAllDataList.add(goods1);
        mAllDataList.add(goods2);
        mAllDataList.add(goods3);
    }

    /**
     * 过滤数据
     */
    private void filterData() {
        if (mSearchKeyword.isEmpty()) {
            mAdapter.setData(mAllDataList);
        } else {
            List<DeliveryGoodsBean> filteredList = new ArrayList<>();
            for (DeliveryGoodsBean bean : mAllDataList) {
                if (bean.getGoodsName().contains(mSearchKeyword) || 
                    bean.getGoodsCode().contains(mSearchKeyword)) {
                    filteredList.add(bean);
                }
            }
            mAdapter.setData(filteredList);
        }
        
        // 更新统计
        updateStatistics();
    }

    /**
     * 更新统计信息
     */
    private void updateStatistics() {
        List<DeliveryGoodsBean> currentList = mAdapter.getData();
        
        if (currentList == null || currentList.isEmpty()) {
            mVarietyCountView.setText("0");
            mTotalQuantityView.setText("0");
            mTotalAmountView.setText("¥0.00");
            return;
        }
        
        // 品种数量：就是商品的条数
        int varietyCount = currentList.size();
        
        // 发货总数量：所有商品的发货数量总和
        int totalQuantity = 0;
        
        // 发货总金额：所有商品的（数量 × 单价）总和
        double totalAmount = 0.0;
        
        for (DeliveryGoodsBean bean : currentList) {
            totalQuantity = totalQuantity + bean.getDeliveryQuantity();
            totalAmount = MathUtils.add(totalAmount, bean.getDeliveryAmount());
        }
        
        mVarietyCountView.setText(String.valueOf(varietyCount));
        mTotalQuantityView.setText(String.valueOf(totalQuantity));
        mTotalAmountView.setText("¥" + String.format("%.2f", totalAmount));
    }
}

